package com.hnzq.cloud.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnzq.cloud.entity.FreFlag;
import com.hnzq.cloud.entity.FreGoodsFlag;
import com.hnzq.cloud.exception.FreFlagException;
import com.hnzq.cloud.mapper.FreFlagMapper;
import com.hnzq.cloud.mapper.FreGoodsFlagMapper;
import com.hnzq.cloud.param.UpdateFreFlagParam;
import com.hnzq.cloud.service.FreFlagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jams
 * @since 2022-02-06
 */
@Service
public class FreFlagServiceImpl extends ServiceImpl<FreFlagMapper, FreFlag> implements FreFlagService {

    @Resource
    private FreGoodsFlagMapper freGoodsFlagMapper;

    @Override
    public Boolean addFreFlag(String flagName) {
        if (flagName==null||flagName.trim().equals("")) throw new FreFlagException("标签名为空",544);
        QueryWrapper<FreFlag> wrapper = new QueryWrapper<>();
        wrapper.eq("flag_name",flagName);
        FreFlag flag = getOne(wrapper);
        if (flag!=null) throw new FreFlagException("该标签名已存在，请重新输入",543);
        FreFlag freFlag = new FreFlag();
        freFlag.setFlagName(flagName);
        return save(freFlag);
    }

    @Override
    public Boolean deleteFreFlag(Integer id){
        FreFlag freFlag = getById(id);
        if (freFlag==null) throw new FreFlagException("所要删除的标签不存在",644);
        QueryWrapper<FreGoodsFlag> wrapper = new QueryWrapper<>();
        wrapper.eq("flag_id",id);
        freGoodsFlagMapper.delete(wrapper);
        return removeById(id);
    }

    @Override
    public List<FreFlag> findAllFreFlags() {
        return list();
    }

    @Override
    public Boolean updateFreType(UpdateFreFlagParam updateFreFlagParam){
        if (updateFreFlagParam.getFlagName().trim().equals("")){
            throw new FreFlagException("修改后的标签名不能为空",544);
        }
        FreFlag flag = getById(updateFreFlagParam.getFlagId());
        if (flag==null) throw new FreFlagException("所修改的标签不存在",644);
        if (flag.getFlagName().trim().equals(updateFreFlagParam.getFlagName().trim())){
            throw new FreFlagException("修改前后的标签名不能一致",543);
        }
        flag.setFlagName(updateFreFlagParam.getFlagName());
        return saveOrUpdate(flag);
    }
}
